
from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
from django.utils.timezone import now
from django.core.cache import cache
from .util import JwtToken

import time


# jwt自定义验证
# 登录认证
class LoginAuth(BaseAuthentication):
    def authenticate(self, request):
        # print('auth--data', request.META.get('HTTP_AUTHORIZATION'))
        try:
            # 获取请求头里的token
            token = request.META.get('HTTP_AUTHORIZATION', None)
            # 解密解签token
            jwt_token = JwtToken()
            userToken = jwt_token.decode_token(data=token)
            return userToken['uuid']
        # # token过期抛出异常
        # except jwt.exceptions.JWTDecodeError:
        #     raise AuthenticationFailed({"code": 1020, "error": "token过期"})
        # 遇到错误的token，或者未携带token，会抛出异常
        except:
            raise AuthenticationFailed({"code": 1020, "error": "错误的token或未携带token"})
